<?php

$sqlfile = './data.sql';
file_put_contents($sqlfile, '');
$fsi = new FilesystemIterator('./');
foreach ($fsi as $fileInfo) {
    if (in_array($fileInfo->getFilename(), ['.', '..'])) {
        continue;
    }
    if ($fileInfo->isDir()) {
        $fsi2 = new FilesystemIterator('./' . $fileInfo->getFilename());
        foreach ($fsi2 as $fileInfo2) {
            $filename = $fileInfo2->getFilename();
            if (strpos($filename, '.php')) {
                $config = require $fileInfo2->getRealPath();
                $list = $config['data'];
                $append = $config['append'] ?? false;
                $isTruncate = $config['truncate'] ?? false;
                $isTree = $config['is_tree'] ?? false;
                $startId = $config['start_id'] ?? 0;
                echo '处理：', ($config['text'] ?? $filename), ' [', $fileInfo2->getPath(), ']', PHP_EOL;
                file_put_contents($sqlfile, '-- ' . ($config['text'] ?? $filename) . PHP_EOL, FILE_APPEND);
                if ($isTruncate) {
                    file_put_contents($sqlfile, ('truncate table ' . ($config['table'] ?? '{table}') . ';' . PHP_EOL), FILE_APPEND);
                }
                if ($isTree) {
                    $id = $startId;
                    $pid = 0;
                    $pid2 = 0;
                    $pid3 = 0;
                    $sqlValues = [];
                    $idArr = [];
                    foreach ($list as $row) {
                        $id++;
                        $idArr[] = $id;
                        $sqlValues = $row;
                        $sqlValues['id'] = $id;
                        $sqlValues['pid'] = $pid;
                        if (isset($sqlValues['children'])) {
                            unset($sqlValues['children']);
                        }
                        if ($append) {
                            $sqlValues = array_merge($sqlValues, $append);
                        }
                        $sql = 'insert into `' . $config['table'] . '` (`' . implode('`, `', array_keys($sqlValues)) . '`) values (\'' . implode('\', \'', array_values($sqlValues)) . '\');' . PHP_EOL;
                        file_put_contents($sqlfile, $sql, FILE_APPEND);
                        if (!isset($row['children'])) {
                            continue;
                        }
                        $pid2 = $id;
                        foreach ($row['children'] as $row2) {
                            $id++;
                            $idArr[] = $id;
                            $sqlValues = $row2;
                            $sqlValues['id'] = $id;
                            $sqlValues['pid'] = $pid2;
                            if (isset($sqlValues['children'])) {
                                unset($sqlValues['children']);
                            }
                            if ($append) {
                                $sqlValues = array_merge($sqlValues, $append);
                            }
                            $sql = 'insert into `' . $config['table'] . '` (`' . implode('`, `', array_keys($sqlValues)) . '`) values (\'' . implode('\', \'', array_values($sqlValues)) . '\');' . PHP_EOL;
                            file_put_contents($sqlfile, $sql, FILE_APPEND);
                            if (!isset($row2['children'])) {
                                continue;
                            }
                            $pid3 = $id;
                            foreach ($row2['children'] as $row3) {
                                $id++;
                                $idArr[] = $id;
                                $sqlValues = $row3;
                                $sqlValues['id'] = $id;
                                $sqlValues['pid'] = $pid3;
                                if (isset($sqlValues['children'])) {
                                    unset($sqlValues['children']);
                                }
                                if ($append) {
                                    $sqlValues = array_merge($sqlValues, $append);
                                }
                                $sql = 'insert into `' . $config['table'] . '` (`' . implode('`, `', array_keys($sqlValues)) . '`) values (\'' . implode('\', \'', array_values($sqlValues)) . '\');' . PHP_EOL;
                                file_put_contents($sqlfile, $sql, FILE_APPEND);
                            }
                        }
                    }
                    if (isset($config['update_sql'])) {
                        $sql = trim(str_replace('__ids__', implode(',', $idArr), $config['update_sql'])) . PHP_EOL;
                        file_put_contents($sqlfile, (rtrim($sql, ';') . ';' . PHP_EOL), FILE_APPEND);
                    }
                    unset($idArr);
                    unset($sqlValues);
                } else {
                    foreach ($list as $row) {
                        if ($append) {
                            $row = array_merge($row, $append);
                        }
                        $sql = 'insert into `' . $config['table'] . '` (`' . implode('`, `', array_keys($row)) . '`) values (\'' . implode('\', \'', array_values($row)) . '\');' . PHP_EOL;
                        file_put_contents($sqlfile, $sql, FILE_APPEND);
                    }
                }
                if (isset($data)) {
                    // echo 'unset data.';
                    unset($data);
                }
                unset($list);
                unset($config);
            }
        }
    }
}
echo 'success!';
